package com.leruan.aasimp.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.dao.EauserDao;
import com.leruan.aasimp.entity.CarInfo;
import com.leruan.aasimp.entity.Eauser;

public class EauserDaoImpl extends HibernateDaoSupport implements EauserDao{

	public void addEauser(Eauser eauser) {
		Session session = this.getSession();
		session.save(eauser);
	}

	public Eauser getEauserByUn(String username) {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(Eauser.class);
		Criterion  c = Expression.eq("username", username);
		Criterion  c1 = Expression.ne("userType", 0l);
		cri.add(c);
		cri.add(c1);
		Eauser result = (Eauser) cri.uniqueResult();
		return result;
	}

	public void updateEauser(Eauser eauser) {
		Session session = this.getSession();
		session.update(eauser);
	}

	public Eauser getEauserByUid(String userid) {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(Eauser.class);
		Criterion  c = Expression.eq("userId", userid);
		cri.add(c);
		Eauser result = (Eauser) cri.uniqueResult();
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<Eauser> getEausersByType(int type) {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(Eauser.class);
		Criterion  c = Expression.eq("userType", Long.parseLong(type+""));
		cri.add(c);
		List<Eauser> result = cri.list();
		return result;
	}

	public void destroyEauser(String userId) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("delete from Eauser where userId=?");
		session.createQuery(sb.toString()).setString(0, userId);
	}

	public List<Eauser> getEausersByTypeAndId(int i, Long idCompany) {
		Session session = this.getSession();
		Criteria cri = session.createCriteria(Eauser.class);
		Criterion  c = Expression.eq("userType", Long.parseLong(i+""));
		Criterion  c1 = Expression.eq("idCompany", idCompany);
		cri.add(c);
		cri.add(c1);
		List<Eauser> result = cri.list();
		return result;
	}

}
